function withData(param) {
  return param < 10 ? '0' + param : '' + param;
}

function getLoopArray(start, end) {
  var start = start || 0;
  var end = end || 1;
  var array = [];
  for (var i = start; i <= end; i++) {
    array.push(withData(i));
  }
  return array;
}

function getMonthDay(year, month) {
  var flag = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0),
    array = null;

  switch (month) {
    case '01':
    case '03':
    case '05':
    case '07':
    case '08':
    case '10':
    case '12':
      array = getLoopArray(1, 31)
      break;
    case '04':
    case '06':
    case '09':
    case '11':
      array = getLoopArray(1, 30)
      break;
    case '02':
      array = flag ? getLoopArray(1, 29) : getLoopArray(1, 28)
      break;
    default:
      array = '月份格式不正确，请重新输入！'
  }
  return array;
}

function getNewDateArry(date, x1) {
  // 当前时间的处理
  var newDate = new Date();
  if (date) {
    newDate = new Date(date.replace(/-/g, "/"));
  }
  var year = withData(newDate.getFullYear()),
    mont = withData(newDate.getMonth() + 1),
    date = withData(newDate.getDate()),
    hour = withData(newDate.getHours()),
    seco = withData(newDate.getSeconds());
  if (x1 && x1 > 0) {
    var minu = '' + Math.round(newDate.getMinutes() / 10);
  } else {
    var minu = withData(newDate.getMinutes());
  }

  return [year, mont, date, hour, minu, seco];
}

function dateTimePicker(startYear, endYear, date, x1) {
  // 返回默认显示的数组和联动数组的声明
  var dateTime = [],
    dateTimeArray = [
      [],
      [],
      [],
      [],
      [],
      []
    ];
  var start = startYear || 1978;
  var end = endYear || 2100;
  // 默认开始显示数据
  var defaultDate = date ? [...date.split(' ')[0].split('-'), ...date.split(' ')[1].split(':')] : getNewDateArry('', x1);
  // 处理联动列表数据
  /*年月日 时分秒*/
  dateTimeArray[0] = getLoopArray(start, end);
  dateTimeArray[1] = getLoopArray(1, 12);
  dateTimeArray[2] = getMonthDay(defaultDate[0], defaultDate[1]);
  dateTimeArray[3] = getLoopArray(0, 23);
  if (x1 && x1 > 0) {
    dateTimeArray[4] = ['00', '10', '20', '30', '40', '50'];
  } else {
    dateTimeArray[4] = getLoopArray(0, 59);
  }
  dateTimeArray[5] = getLoopArray(0, 59);

  dateTimeArray.forEach((current, index) => {
    if (x1 && x1 > 0 && index == 4) {
      dateTime.push(defaultDate[index] == 6 ? 5 : defaultDate[index]);
    } else {
      dateTime.push(current.indexOf(defaultDate[index]));
    }
  });

  return {
    dateTimeArray: dateTimeArray,
    dateTime: dateTime
  }
}

/**
 * 返回时间下标结果集
 */
function getDateArry(dateTimeArray, date) {
  let dateArry = []
  let arry = getNewDateArry(date);
  dateTimeArray.forEach((current, index) => {
    dateArry.push(current.indexOf(arry[index]));
  });
  return dateArry;
}



/**
 * 转换为日期时间格式显示
 */
function formatterDate(arr, d, length) {
  if (!arr[0]) {
    return "";
  }
  if (!arr[3]) {
    return arr[0][d[0]] + "-" + arr[1][d[1]] + "-" + arr[2][d[2]];
  }
  if (length == 4) {
    return arr[0][d[0]] + "-" + arr[1][d[1]] + "-" + arr[2][d[2]] + " " + arr[3][d[3]] + ":" + arr[4][d[4]]
  } else {
    if (!arr[5]) {
      return arr[0][d[0]] + "-" + arr[1][d[1]] + "-" + arr[2][d[2]] + " " + arr[3][d[3]] + ":" + arr[4][d[4]] + ":00";
    }
    return arr[0][d[0]] + "-" + arr[1][d[1]] + "-" + arr[2][d[2]] + " " + arr[3][d[3]] + ":" + arr[4][d[4]] + ":" + arr[5][d[5]];
  }
}

/**
 * 比较大小，d1>d2返回1，小于返回-1，等于返回0
 */
function compareTo(d1, d2) {
  //日期格式化
  let time1 = new Date(d1.replace(/-/g, "/"));
  let time2 = new Date(d2.replace(/-/g, "/"));
  if (time1.getTime() > time2.getTime()) {
    return 1;
  } else if (time1.getTime() < time2.getTime()) {
    return -1;
  } else {
    return 0;
  }
}

/**
 * 比较大小，d2>d1返回1，小于返回-1，等于返回0
 */
function compare(d1, d2) {
  //日期格式化
  d1 = new Date(d1.replace(/-/g, "/"));
  d2 = new Date(d2.replace(/-/g, "/"));
  if (d2.getTime() > d1.getTime()) {
    return 1;
  } else if (d2.getTime() < d1.getTime()) {
    return -1;
  } else {
    return 0;
  }
}

/**
 * 计算时间差，d2>d1
 */
function timeDiff(d1, d2) {
  d1 = new Date(d1.replace(/-/g, "/"));
  d2 = new Date(d2.replace(/-/g, "/"));
  var d = d2.getTime() - d1.getTime();
  var days = Math.floor(d / (1000 * 60 * 60 * 24));
  var hours = Math.floor((d % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  return days + '天' + hours + '小时';
}

/**
 * 获取当前日期
 */
function getNewDate() {
  var newDate = new Date();
  return newDate.getFullYear() + '-' + withData(newDate.getMonth() + 1) + '-' + withData(newDate.getDate())
}

/**
 * 获取当前年月 yyyy-MM
 */
function getNewMonth() {
  var newDate = new Date();
  return newDate.getFullYear() + '-' + withData(newDate.getMonth() + 1)
}

/**
 * 获取当前年
 */
function getNewYear() {
  var newDate = new Date();
  return newDate.getFullYear()
}

export default {
  dateTimePicker: dateTimePicker,
  getNewDateArry: getNewDateArry,
  getDateArry: getDateArry,
  getMonthDay: getMonthDay,
  formatterDate: formatterDate,
  compare: compare,
  compareTo: compareTo,
  getNewDate: getNewDate,
  getNewMonth: getNewMonth,
  timeDiff: timeDiff,
  getNewYear:getNewYear
}